#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
__author__ : xyh
__date__: 2016-09-14
新三板预披露公告爬虫(源:中小企业股转系统),每日增量更新
"""

import os
import sys
from ConfigParser import ConfigParser
from datetime import datetime

from scpy.logger import get_logger
from xtls.timeparser import parse_time
from crawler.artificial_pre_xinsanban_anno import PreXsbCrawler
from queue.anno_producer import BuildPreXsb
from queue.anno_producer import produce_message

reload(sys)
sys.setdefaultencoding("utf-8")
logger = get_logger(__file__)


def crawler():
    PreXsbCrawler().run()


def publish_message(STARTUP_TIME):
    config = ConfigParser()
    config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'config.cfg'))
    with open(config_path, 'rb') as cfgfile:
        config.readfp(cfgfile)

    amqp_url = 'amqp://sc-admin:1qaz2wsx@10.51.29.242:5672/%2F?connection_attempts=3&heartbeat_interval=3600'
    queue_name = config.get('queue', 'daily_queue_name')
    start_anno_time = parse_time(u'1天前')
    stop_anno_time = parse_time(u'1秒前')
    build_message = BuildPreXsb(start_anno_time=start_anno_time, stop_anno_time=stop_anno_time,
                                STARTUP_TIME=STARTUP_TIME)

    produce_message(amqp_url, queue_name, build_message)


def main():
    STARTUP_TIME = datetime.today()  # 程序启动时间 eg: datetime.datetime(2016, 9, 14, 13, 53, 4)
    crawler()
    publish_message(STARTUP_TIME)


if __name__ == '__main__':
    main()


